/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.daolayer;

import fri.businessLayer.UserBeanRemote;
import fri.businessLayer.UserMoneyBeanRemote;
import fri.entiry.FrCurrency;
import fri.entiry.FrUser;
import fri.entiry.FrUsermoney;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author NguyenDongUIT
 */
public class UserMoneyDAO {
    
    public static List<FrUsermoney> getAllUserMoney() {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.getAllUserMoney();

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
        
    } 
    public static List<FrUsermoney> getUserMoneyByEmail(String email)
    {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.getUserMoneyByEmail(email);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static List<FrUsermoney> getUserMoneyByUserCode(String userCode)
    {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.getUserIdByUserCode(userCode);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static List<FrUsermoney> getUserMoneyByUserId(String userId)
    {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.getUserMoneyByUserId(userId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static int addUserMoney(String userId, int currencyId )
    {
        try {
            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            FrUsermoney userMoney=new FrUsermoney();
            
            UUID u=UUID.randomUUID();
            userMoney.setIdUserMoney(u.toString());
            FrUser user=new FrUser();
            FrCurrency cur=new FrCurrency();
            cur.setCurrencyId(currencyId);
            user.setUserId(userId);
            
            userMoney.setUserId(user);
            userMoney.setCurrencyId(cur);
            
            userMoney.setLastChanged(new Date());
            userMoney.setVal(1000000);
            
            
            
            return rm.addUserMoney(userMoney);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error : " + e.getMessage());
        }
        return 0;
    }
    
    public static int changeUserMoney(String userId, int currencyId, float value)
    {
        try {
            
            FrUsermoney userMoney=new FrUsermoney();
            
            FrUser user=new FrUser();
            FrCurrency cur=new FrCurrency();
            cur.setCurrencyId(currencyId);
            user.setUserId(userId);
            userMoney.setUserId(user);
            userMoney.setCurrencyId(cur);
            userMoney.setVal(value);
            userMoney.setLastChanged(new Date());
            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.changeUserMoney(userMoney);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error : " + e.getMessage());
        }
        return 0;
    }
    
    public static int subtractUserMoney(String userId, float value, int currencyId) {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.subtractUserMoney(userId, value, currencyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
    public static FrUsermoney getUserMoneyByUICurrency(String userId, int currencyId) {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.getUserMoneyByUICurrency(userId, currencyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }
    
    public static boolean checkUserMoneyAvailable(String userId, float value, int currencyId)
    {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.checkUserMoneyAvailable(userId, value, currencyId);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return false;
    }
    
    
    public static int deleteUserMoney(String id)
    {
        try {

            Context ic = new InitialContext();
            UserMoneyBeanRemote rm = (UserMoneyBeanRemote) ic.lookup("fri.businessLayer.UserMoneyBeanRemote");
            return rm.deleteUserMoney(id);

        } catch (Exception e) {

            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return 0;
    }
    
}
